﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ITOMMS.Entity;
using Watson.Core.Tools;
using ITOMMS.Business;

namespace ITOMMS.Web.Main
{
    public partial class DirectionsAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    string strId = Request.QueryString["id"].ToString();
                    int id = 0;
                    if (int.TryParse(strId, out id))
                    {
                        dataBind(id);
                    }

                    if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.技术员.ToString() || SessionHelper.UserRoleGroup == PubEnums.RoleGroup.技术主管.ToString())
                    {
                        this.txtTitle.ReadOnly = true;
                        this.FreeTextBox1.ReadOnly = true;
                        this.btnSave.Enabled = false;
                    }     
                }
            }
        }

        private void dataBind(int id)
        {
            tbCustomerNeedToKnowEntity needToKnow = new Entity.tbCustomerNeedToKnowEntity();
            needToKnow = needToKnow.GetById(id);
            if (needToKnow != null)
            {
                ViewState["Entity"] = needToKnow;
                this.txtTitle.Text = needToKnow.Title.Trim();
                this.FreeTextBox1.Text = needToKnow.Content;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                tbCustomerNeedToKnowEntity entity = new tbCustomerNeedToKnowEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbCustomerNeedToKnowEntity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Now;
                }


                entity.Title = this.txtTitle.Text.Trim();
                entity.Content = this.FreeTextBox1.Text;
                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Now;
                entity.Update();

                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
        }

    }
}
